/*
 * (C) Copyright 2005 Davide Brugali, Marco Torchiano
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307  USA
 */

package model.environment;

/**
 * Title:        The MObile RObot Simulation Environment
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      Universit� di Bergamo
 *
 * @author Davide Brugali
 * @version 1.0
 */

public class Platform extends Device {
	private static final int CLOCKWISE_ORIENTATION = 1;
	private static final int ANTICLOCKWISE_ORIENTATION = -1;

	private static final double ONE_DEGREE = 1.0;

	private int orientation;
	private double rotationStep;
	private double moveStep;
	private double numRotationSteps;
	private double numMoveSteps;

	public Platform(String name, MobileRobot robot, Environment environment) {
		super(name, robot, new Position(0.0, 0.0, 0.0), environment);

		this.orientation = CLOCKWISE_ORIENTATION;
		this.rotationStep = ONE_DEGREE;
		this.moveStep = ONE_DEGREE;

		this.numRotationSteps = 0;
		this.numMoveSteps = 0;

		this.addPoint(20, 20);
		this.addPoint(30, 10);
		this.addPoint(30, -10);
		this.addPoint(20, -20);
		this.addPoint(-20, -20);
		this.addPoint(-20, 20);
	}

	public void executeCommand(String command) {
		if (command.contains("ROTATERIGHT")) {
			double angle = Math.abs(Double.parseDouble(command.trim().substring(12).trim()));
			numRotationSteps = angle / rotationStep;
			orientation = CLOCKWISE_ORIENTATION;
			running = true;
		} else if (command.contains("ROTATELEFT")) {
			double angle = Math.abs(Double.parseDouble(command.trim().substring(11).trim()));
			numRotationSteps = angle / rotationStep;
			orientation = ANTICLOCKWISE_ORIENTATION;
			running = true;
		} else if (command.contains("MOVEFW")) {
			double distance = Math.abs(Double.parseDouble(command.trim().substring(7).trim()));
			numMoveSteps = distance / moveStep;
			orientation = CLOCKWISE_ORIENTATION;
			running = true;
		} else if (command.contains("MOVEBW")) {
			double distance = Math.abs(Double.parseDouble(command.trim().substring(7).trim()));
			numMoveSteps = distance / moveStep;
			orientation = ANTICLOCKWISE_ORIENTATION;
			running = true;
		} else
			writeOut("DECLINED");
	}

	public void nextStep() {
		if (!running) {
			return;
		}
		if (numRotationSteps > 0.0) {
			if (numRotationSteps < 1.0) {
				robotPosition.rotateAroundAxis(0.0, 0.0, numRotationSteps * orientation * rotationStep);
			} else {
				robotPosition.rotateAroundAxis(0.0, 0.0, orientation * rotationStep);
			}
			// updates the robot's position
			robot.writePosition(robotPosition);
			// redrwaws the environment
			environment.repaint();
			numRotationSteps -= 1.0;
		} else if (numMoveSteps > 0.0) {
			if (numMoveSteps < 1.0) {
				robotPosition.rotateAroundAxis(numMoveSteps * moveStep * orientation, 0.0, 0.0);
			} else {
				robotPosition.rotateAroundAxis(moveStep * orientation, 0.0, 0.0);
			}
			// updates the robot's position
			robot.writePosition(robotPosition);
			// redraws the environment
			environment.repaint();
			numMoveSteps -= 1.0;

		} else {
			running = false;
			writeOut("PLATFORM ARRIVED");
		}
	}

}
